package com.china08.yxyapi.api.manage.tec.teaching.lecture.pack;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePack.Res;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureRes;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePackRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureResRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 老师 ---备授课--授课包详情
 * 
 */
@CrossOrigin
@RestController
public class LectureGetPackController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePackRepository yxyLecturePackRepository;

    @Autowired
    private YxyLectureResRepository yxyLectureResRepository;

    @Autowired
    private YxyResResourceRepository YxyResResourceRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/pack/get/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "id") String id) {
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String userVIP = getProfile(request, response).getResVip();

        YxyLecturePack lecturePack = yxyLecturePackRepository.findByIdAndUserIdAndSchoolId(id, userId, schoolId);

        Assert.notNull(lecturePack, "授课包找不到了，请刷新页面");
        List<Res> listPrePackRes = lecturePack.getResources();
        List<ResModel> resModels = new ArrayList<LectureGetPackController.ResModel>();
        String resId = "";
        RespModel respModel = new RespModel();
        boolean use = lecturePack.isUse();
        if (use) {
            respModel.used = "1";
        } else {
            respModel.used = "0";
        }
        for (Res res : listPrePackRes) {
            ResModel model = new ResModel();
            List<Integer> resUsed = new ArrayList<>();
            model.resourceId = res.getResourceId();
            model.formatId = res.getFormatId();
            model.title = res.getResourceName();
            model.courseType = res.getCourseType();
            model.orderNum = res.getOrderNum() != null ? res.getOrderNum() : 0;
            Integer fileSize = res.getFileSize();

            YxyResResource yxyResResource = YxyResResourceRepository.findById(res.getResourceId());
            if (null != yxyResResource) {
                model.isHighQualityYx = yxyResResource.getIsHighQualityYx();
                model.vip = yxyResResource.getIsVip();
            } else {
                model.isHighQualityYx = 0;
                model.vip = false;
            }
            model.userVIP = userVIP;
            if (null != fileSize) {
                Double valueOf = Double.valueOf(fileSize + "");
                Double file = valueOf / 1024;
                BigDecimal bg = new BigDecimal(file);
                double f1 = bg.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
                model.fileSize = f1;
            } else {
                model.fileSize = 0.0;

            }
            Integer usesHomework = res.getUsesHomework();
            if (usesHomework == 1) {
                usesHomework = 3;
            } else {
                usesHomework = 5;
            }
            Integer usesProview = res.getUsesProview();
            if (usesProview == 1) {
                usesProview = 2;
            } else {
                usesProview = 5;
            }
            Integer usesTeaching = res.getUsesTeaching();
            if (usesTeaching == 1) {
                usesTeaching = 1;
            } else {
                usesTeaching = 5;
            }
            Integer usesTest = res.getUsesTest();
            if (usesTest == 1) {
                usesTest = 4;
            } else {
                usesTest = 5;
            }
            resUsed.add(usesTeaching);
            resUsed.add(usesProview);
            resUsed.add(usesHomework);
            resUsed.add(usesTest);
            model.resUsed = resUsed;
            if (model.courseType == 0) {
                List<String> quesIds = new ArrayList<String>();
                resId = res.getResourceId();
                YxyLectureRes lectureRes = yxyLectureResRepository.findByResourceId(resId);
                quesIds = lectureRes.getQuesIds();
                respModel.quesIds = model.quesIds;
                model.quesIds = quesIds;
            }

            resModels.add(model);
        }

        resModels = resModels.stream().sorted((p1, p2) -> p1.orderNum.compareTo(p2.orderNum))
                .collect(Collectors.toList());
        respModel.id = lecturePack.getId();
        respModel.title = lecturePack.getPackName();
        respModel.count = listPrePackRes.size();
        respModel.schoolNick = lecturePack.getSchoolNick();
        respModel.userNick = lecturePack.getUserNick();
        respModel.createdDateFormat = DateToolUtils.format(lecturePack.getPreDate(), DateToolUtils.FORMAT_SHORT_YMD);
        respModel.resources = resModels;
        respModel.classNick = lecturePack.getClassName();
        respModel.bookContentName = lecturePack.getBookcontentsName();
        respModel.subjectName = lecturePack.getSubjectName();
        respModel.userVIP = userVIP;
        return respModel;
    }

    protected class ResModel {
        public String resourceId;
        public String formatId;
        public String title;
        public Integer courseType;
        public List<String> quesIds;
        public Integer orderNum;
        public Double fileSize;
        public List<Integer> resUsed;
        public boolean vip;
        public Integer isHighQualityYx;
        public String userVIP;
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String title;
        public int count;
        // 贡献者学校名称
        public String schoolNick;
        // 贡献者名称
        public String userNick;
        // 创建时间（收录时间）
        public String createdDateFormat;
        // 资源
        public List<ResModel> resources;
        public String classNick;
        public String bookContentName;
        public String subjectName;
        public List<String> quesIds;
        public String userVIP;
        public String used;

    }
}
